// 计算图片轮播区域的宽度和高度，以便图片能够等比例缩放
var ratio = 440/750;
var w = document.documentElement.getBoundingClientRect().width;
var h = parseInt(w*ratio, 10);
var carousel =document.getElementById('carousel');
carousel.style.height = h + 'px';
// 切换滚动
var gallery = carousel.querySelector('.gallery');
var leftArrow = carousel.querySelector('.left.arrow');
var rightArrow = carousel.querySelector('.right.arrow');
var pics = 5;
var current = 0;
var slideIntervalId = null;
function setTraslate(v) {
  gallery.style.transform = 'translate3d('+v+ 'px, 0, 0)';
}
function handleLeftArrowClick() {
  clearInterval(slideIntervalId);
  gallerySlideLeft();
  slideIntervalId = setInterval(gallerySlideRight, 3000);
}
function handleRightArrowClick() {
  clearInterval(slideIntervalId);
  gallerySlideRight();
  slideIntervalId = setInterval(gallerySlideRight, 3000);
}
function gallerySlideLeft() {
  if (current > 0) {
    current--;
  } else {
    current = 4;
  }
  setTraslate(-w*current);
}
function gallerySlideRight() {
  if (current < 4) {
    current++;
  } else {
    current = 0;
  }
  setTraslate(-w*current);
}

function getGalleryImgSrc() {
  var srcList = [];
  var imgs = gallery.querySelectorAll('img');
  for (var i = 0, len = imgs.length; i < len; i++) {
    srcList.push(imgs[i].src);
  }
  return srcList;
}
var imgSrcList = getGalleryImgSrc();
//这个是调用微信图片浏览器的函数
function imagePreview(curSrc, srcList) {
  if (!curSrc || !srcList || srcList.length == 0) {
    return;
  }
  if (WeixinJSBridge) {
    WeixinJSBridge.invoke('imagePreview', {
      'current': curSrc,
      'urls': srcList
    });
  }
}

function scaleImg(e) {
  imagePreview(e.target.src, imgSrcList);
}

leftArrow.addEventListener('click', handleLeftArrowClick);
rightArrow.addEventListener('click', handleRightArrowClick);
// 启动定时器
slideIntervalId = setInterval(gallerySlideRight, 3000);
// 点击图片的时候需要放大
gallery.addEventListener('click', scaleImg);